// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.0
import "verbs.js" as Verbs

Rectangle
{
    id: verb
    width: page.width
    height: page.height
    color: "black"
    property int finished: 0
    property int currentCounter: 0
    property int length: Verbs.verbs.length

    Repeater
    {
	id: verbs
	model: Verbs.verbs.length
        Verbword{name: Verbs.verbs[index]; visible: true; y: page.height}

    }

    Verbword{id: cross; name: "cross.jpg"; visible: true; y: page.height}
    Verbword{id: rest; name: "rest.jpg"; visible: true; y: page.height}
    Verbword{id: finished; name: "finished.jpg"; visible: true; y: page.height}


    function shuffle(o)
    {
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
    }

    Component.onCompleted:
    {
	for (var i=0; i<Verbs.verbs.length; ++i)
	{
	    Verbs.order.push(i);
	    Verbs.order = shuffle(Verbs.order);
	}
    }

    MouseArea
    {
	anchors.fill: parent
	onClicked:
	{
	    if (verb.finished == 1)
	    {
		console.log("finished, quiting")
		Qt.quit();
	    }
	}

    }

    function timeTick(type)
    {
	if (type == "cross")
	{

	    if (currentCounter == page.picturesToShow)
	    {
		page.mainTimerRunning = false;
		page.event("finished")
		finished.y = 0;
		verb.finished = 1;
		return;

	    }

            rest.y = page.height;
	    cross.y = 0;
	    page.event(type)
	}
	else if (type == "picture")
	{
            cross.y = page.height;
	    if (currentCounter != 0)
	    {
                children[Verbs.order[currentCounter-1]].y = page.height;
	    }

	    children[Verbs.order[currentCounter]].y = 0;

	    page.event("verb_"+children[Verbs.order[currentCounter]].name);
	    ++currentCounter;

	}

	else if (type == "rest")
	{

	    rest.y = 0;
	    page.event(type)
	}

    }

}
